﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using IPMsgExpections;
using CommonUtility;

namespace MessageCenter
{
    public class MessageSendMsg : MessageModel
    {
        //是否需要返回确认收到消息
        private bool isSendCheckOpt;

        //是否是封装消息
        private bool isSecretOpt;

        private IPMsgFileInfo fileInfo;

        //获取或者设置是否需要返回确认消息
        public bool IsSendCheckOpt
        {
            get { return isSendCheckOpt; }
            set { isSendCheckOpt = value; }
        }

        //获取或者设置是否为封装消息
        public bool IsSecretOpt
        {
            get { return isSecretOpt; }
            set { isSecretOpt = value; }
        }

        public IPMsgFileInfo FileInfo
        {
            get { return fileInfo; }
            set { fileInfo = value; }
        }

        //构造函数
        public MessageSendMsg(
            string userName,
            string hostName,
            uint indexPackage,
            string attachMsg,
            IPMsgFileInfo fileToSend,
            bool checkOPT,
            bool secretOPT)
            : base(userName, hostName, indexPackage)
        {
            this.isSendCheckOpt = checkOPT;
            this.isSecretOpt = secretOPT;
            this.fileInfo = fileToSend;

            uint commandwordTmp = 0;
            commandwordTmp = ConstWords.CommonCommand.IPMSG_SENDMSG;

            if (checkOPT)
            {
                //需要回复，命令字中加入IPMSG_SENDCHECKOPT
                commandwordTmp = commandwordTmp | ConstWords.OptionForSendCommond.IPMSG_SENDCHECKOPT;
            }

            if (secretOPT)
            {
                commandwordTmp = commandwordTmp | ConstWords.OptionForSendCommond.IPMSG_SECRETOPT;
            }

            if (this.fileInfo != null)
            {
                commandwordTmp = commandwordTmp | ConstWords.OptionForAllCommond.IPMSG_FILEATTACHOPT;
                base.CommandWords = commandwordTmp;
                base.AttachMessages = attachMsg + ConstWords.IPMSG_ENDOFMSG + this.fileInfo.ConvertToAttachMessage();
            }
            else
            {
                base.CommandWords = commandwordTmp;
                base.AttachMessages = attachMsg + ConstWords.IPMSG_ENDOFMSG;
            }
        }

        //构造方法
        internal MessageSendMsg(
            uint version,
            uint indexPack,
            string userName,
            string hostName,
            uint commondWord,
            string attachMessages)
            : base(version, indexPack, userName, hostName, attachMessages)
        {
            this.CommandWords = commondWord;
            switch (CommandWords & (uint)0x300) //同二进制0011 0000 0000做与运算
            {
                //IPMSG_SECRETOPT位为1
                case ConstWords.OptionForSendCommond.IPMSG_SECRETOPT:
                    this.isSecretOpt = true;
                    break;

                //IPMSG_SENDCHECKOPT为1
                case ConstWords.OptionForSendCommond.IPMSG_SENDCHECKOPT:
                    this.isSendCheckOpt = true;
                    break;

                //IPMSG_SECRETOPT与IPMSG_SENDCHECKOPT均为1
                case ConstWords.OptionForSendCommond.IPMSG_SECRETOPT | ConstWords.OptionForSendCommond.IPMSG_SENDCHECKOPT:
                    this.isSecretOpt = true;
                    this.isSendCheckOpt = true;
                    break;

                //IPMSG_SECRETOPT与IPMSG_SENDCHECKOPT均不为1
                default:
                    break;
            }
        }
    }
}
